package com.im.app.mybatis;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransaction;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Component
public class MybatisConfig {

    public static final String url = "jdbc:mysql://rm-3ns6tu9dqkznkw3mg1o.mysql.rds.aliyuncs.com:3306/chat_im?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    public static final String usr = "chat_yun";
    public static final String password = "RSdTUMLX7f";

    public Configuration configuration;
    public JdbcTransaction jdbcTransaction;


    public void init() throws SQLException {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = factoryBuilder.build(MybatisConfig.class.getResourceAsStream("/mybatis-config.xml"));
        configuration = factory.getConfiguration();
        Connection connection = DriverManager.getConnection(url, usr, password);
        jdbcTransaction = new JdbcTransaction(connection);
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public JdbcTransaction getJdbcTransaction() {
        return jdbcTransaction;
    }

    public void setJdbcTransaction(JdbcTransaction jdbcTransaction) {
        this.jdbcTransaction = jdbcTransaction;
    }
}
